Skip to content

Provide fallback for Observation KeyValues #5020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jonatan-ivanov
Copy link
Member

@jonatan-ivanov jonatan-ivanov commented Jul 17, 2025

Similarly to #4994 this PR adds a fallback value to all the KeyValues used by DefaultMongoHandlerObservationConvention.

TODO:

  • Read the Spring Data contribution guidelines
  • Use the code formatters provided here
  • Add test cases
  • Added myself as author in the headers of the classes I touched
  • Maybe rebasing it on top of 4.4.x

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jul 17, 2025
* @return key value
*/
public KeyValue withOptionalValue(@Nullable String value) {
return withValue(ObjectUtils.isEmpty(value) ? KeyValue.NONE_VALUE : value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if "UNKNOWN" is a better default value for most of these rather than none.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to have a bit more encapsulated API for a more declarative approach to reduce the likelihood for using the API wrong. Something along the lines of:

// TODO: it would be nice to have a getKeyName() method returning KeyName from KeyValue for usage in e.g. KeyName[] getLowCardinalityKeyNames() 
static KeyValue DB_SYSTEM = KeyValue.of("db.system", "mongodb");


static SmartKeyName<ConnectionString> DB_CONNECTION_STRING = SmartKeyName.required("db.connection_string",
		ConnectionString::getConnectionString);

later:

@Nullable ConnectionString connectionString = context.getConnectionString();

KeyValues keyValues = KeyValues.of(MongoObservation.DB_SYSTEM,
		MongoObservation.DB_CONNECTION_STRING.valueOf(connectionString));

You can find SmartKeyName (needs probably a better name) at
https://gist.github.com/mp911de/6729ca327326d072820a1a0eb225c4a6

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shakuzen

I wonder if "UNKNOWN" is a better default value for most of these rather than none.

I think so, I can also make the fallback mandatory so it needs to be decided for every key-value.

@mp911de

I would like to have a bit more encapsulated API for a more declarative approach [...]

We can consider improving on this in Micrometer 1.16 (let me talk to @shakuzen about it) but I think this should be considered as a bug and should be fixed in earlier versions where Micrometer 1.16 will not be available I think (unless you want to copy some code from there).

}
}

return keyValues;
return KeyValues.of(
DB_SYSTEM.withValue("mongodb"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your approach and I like it more than concatenating KeyValues. With a refined approach regarding value extraction, we might get a chance for improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: waiting-for-triage An issue we've not yet triaged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants